﻿CREATE PROCEDURE [dbo].[yaf_user_save](
                @UserID         INT,
                @BoardID        INT,
                @UserName       NVARCHAR(50)  = NULL,
                @Password       NVARCHAR(32)  = NULL,
                @Email          NVARCHAR(50)  = NULL,
                @Hash           NVARCHAR(32)  = NULL,
                @Location       NVARCHAR(50)  = NULL,
                @HomePage       NVARCHAR(50)  = NULL,
                @TimeZone       INT,
                @Avatar         NVARCHAR(255)  = NULL,
                @LanguageFile   NVARCHAR(50)  = NULL,
                @ThemeFile      NVARCHAR(50)  = NULL,
                @Approved       BIT  = NULL,
                @MSN            NVARCHAR(50)  = NULL,
                @YIM            NVARCHAR(30)  = NULL,
                @AIM            NVARCHAR(30)  = NULL,
                @ICQ            INT  = NULL,
                @RealName       NVARCHAR(50)  = NULL,
                @Occupation     NVARCHAR(50)  = NULL,
                @Interests      NVARCHAR(100)  = NULL,
                @Gender         TINYINT  = 0,
                @Weblog         NVARCHAR(100)  = NULL,
                @PMNotification BIT  = NULL)
AS
    BEGIN
        DECLARE  @RankID INT
        DECLARE  @Flags INT
        SET @Flags = 0
        IF @Approved <> 0
            SET @Flags = @Flags | 2
        IF @Location IS NOT NULL 
           AND @Location = ''
            SET @Location = NULL
        IF @HomePage IS NOT NULL 
           AND @HomePage = ''
            SET @HomePage = NULL
        IF @Avatar IS NOT NULL 
           AND @Avatar = ''
            SET @Avatar = NULL
        IF @MSN IS NOT NULL 
           AND @MSN = ''
            SET @MSN = NULL
        IF @YIM IS NOT NULL 
           AND @YIM = ''
            SET @YIM = NULL
        IF @AIM IS NOT NULL 
           AND @AIM = ''
            SET @AIM = NULL
        IF @ICQ IS NOT NULL 
           AND @ICQ = 0
            SET @ICQ = NULL
        IF @RealName IS NOT NULL 
           AND @RealName = ''
            SET @RealName = NULL
        IF @Occupation IS NOT NULL 
           AND @Occupation = ''
            SET @Occupation = NULL
        IF @Interests IS NOT NULL 
           AND @Interests = ''
            SET @Interests = NULL
        IF @Weblog IS NOT NULL 
           AND @Weblog = ''
            SET @Weblog = NULL
        IF @PMNotification IS NULL
            SET @PMNotification = 1
        IF @UserID IS NULL 
            OR @UserID < 1
            BEGIN
                IF @Email = ''
                    SET @Email = NULL
                SELECT @RankID = RankID
                FROM   
                    yaf_Rank
                WHERE  (Flags & 1) <> 0
                AND BoardID = @BoardID
                INSERT INTO yaf_User
                           (BoardID,
                            RankID,
                            Name,
                            Password,
                            Email,
                            Joined,
                            LastVisit,
                            NumPosts,
                            Location,
                            HomePage,
                            TimeZone,
                            Avatar,
                            Gender,
                            Flags,
                            PMNotification)
                VALUES     (@BoardID,
                            @RankID,
                            @UserName,
                            @Password,
                            @Email,
                            Getdate(),
                            Getdate(),
                            0,
                            @Location,
                            @HomePage,
                            @TimeZone,
                            @Avatar,
                            @Gender,
                            @Flags,
                            @PMNotification)
                SET @UserID = Scope_identity()
                INSERT INTO yaf_UserGroup
                           (UserID,
                            GroupID)
                SELECT @UserID,
                       GroupID
                FROM   
                    yaf_Group
                WHERE  BoardID = @BoardID
                AND (Flags & 4) <> 0
                IF @Hash IS NOT NULL 
                   AND @Hash <> ''
                   AND @Approved = 0
                    BEGIN
                        INSERT INTO yaf_CheckEmail (UserID, Email, Created, Hash) VALUES (@UserID, @Email, getdate(), @Hash)
                    END
            END
        ELSE
          BEGIN
              UPDATE yaf_User
              SET    Location = @Location,
                     HomePage = @HomePage,
                     TimeZone = @TimeZone,
                     LanguageFile = @LanguageFile,
                     ThemeFile = @ThemeFile,
                     MSN = @MSN,
                     YIM = @YIM,
                     AIM = @AIM,
                     ICQ = @ICQ,
                     RealName = @RealName,
                     Occupation = @Occupation,
                     Interests = @Interests,
                     Gender = @Gender,
                     Weblog = @Weblog,
                     PMNotification = @PMNotification
              WHERE  UserID = @UserID
              IF @Email IS NOT NULL
                  UPDATE yaf_User
                  SET    Email = @Email
                  WHERE  UserID = @UserID
          END
    END


